﻿@model TN_CMS.Domain.Entities.SysRole

@using TN_CMS.Admin.Helper.Common
@using TN_CMS.Framework.Variable

@{
    Layout = "~/Views/Shared/_Admin.cshtml";
}

@Html.Partial("_SystemHeader")
<div id="admin-container">
    <div class="tool-bar">
        <button class="blue" onclick="AdminHelper.LoadDetailData(-1);">Create</button>
        <button class="normal" onclick="AdminHelper.LoadDetailData();">Edit</button>
        <button class="normal" onclick="AdminHelper.UpdateActiveData(true);">Active</button>
        <button class="normal" onclick="AdminHelper.UpdateActiveData(false);">Inactive</button>
        <button class="red" onclick="AdminHelper.DeleteData();">Delete</button>
    </div>
    @Html.Partial("_AdminTable")
</div>
<div id="dialog-model" title="Manage Role" style="display:none">
    @using (Html.BeginForm())
    {
        @Html.EditorForModel()
    }
</div>
<div id="dialog-dynatree" title="Manage Feature" style="display:none">
    <div id="tree"></div>
</div>

<script type='text/javascript'>
    /* =========================== admin =========================== */

    AdminHelper = new MultiAdminHelper();

    AdminHelper.DisplayCallBackSuccess = function (result) {
        var self = this;

        if (result.Message == null || result.Message == "") {
            self.Total = result.Optional;

            var data = new google.visualization.DataTable();

            data.addColumn('number', '@Html.DisplayNameFor(m => m.Id)' + CommonHelper.HiddenInputSorting("column0", "C.Id"));
            data.addColumn('string', '@Html.DisplayNameFor(m => m.Name)' + CommonHelper.HiddenInputSorting("column1", "C.Name"));
            data.addColumn('string', '@Html.DisplayNameFor(m => m.Key)' + CommonHelper.HiddenInputSorting("column2", "C.[Key]"));
            data.addColumn('string', '@Html.DisplayNameFor(m => m.Description)' + CommonHelper.HiddenInputSorting("column3", "C.Description"));
            data.addColumn('string', '@Html.DisplayNameFor(m => m.TotalFeatures)' + CommonHelper.HiddenInputSorting("column4", "disable-sorting"));
            data.addColumn('boolean', '@Html.DisplayNameFor(m => m.IsSystemRole)' + CommonHelper.HiddenInputSorting("column5", "C.IsSystemRole"));
            data.addColumn('boolean', '@Html.DisplayNameFor(m => m.IsActive)' + CommonHelper.HiddenInputSorting("column6", "C.IsActive"));
            data.addColumn('string', '--' + CommonHelper.HiddenInputSorting("column6", "disable-sorting"));

            if (result.Result.length > 0) {
                for (var i = 0; i < result.Result.length; i++) {
                    var item = result.Result[i];

                    var cellName = "<span class='main-text'>" + item.Name + "</span>";
                    var cellTotalFeatures = "<a class='excutable' href='javascript:void(0);' onclick='DynaTreeHelper.LoadManageData(" + item.Id + ")'>View " + item.TotalFeatures + " item(s)</a>";
                    var btnRow = self.GetRowButton({
                        Id: item.Id,
                        Edit: true,
                        Delete: true
                    });

                    var tmpRow = [
                        item.Id,
                        cellName,
                        item.Key,
                        item.Description,
                        cellTotalFeatures,
                        item.IsSystemRole,
                        item.IsActive,
                        btnRow
                    ];

                    data.addRow(tmpRow);
                }
            }
            else {
                CommonHelper.ShowMsg(CommonPhrase.NoData);
            }

            self.Table = new google.visualization.Table($("#" + self.ContainerId + " .table-list")[0]);
            self.DrawTable(data);
        }
        else {
            CommonHelper.ShowMsg("Error: " + result.Message, MsgIconType.Error);
        }
    }

    AdminHelper.LoadDetailUrl = "@Url.Action("GetDetailSysRole")";
    AdminHelper.ManageUrl = "@Url.Action("ManageSysRole")";
    AdminHelper.UpdateActiveUrl = "@Url.Action("UpdateActiveSysRole")";
    AdminHelper.DeleteUrl = "@Url.Action("DeleteSysRole")";
    AdminHelper.CanSort = true;
    AdminHelper.SortColumn = 0;
    AdminHelper.SortColumnName = "C.Id";
    AdminHelper.Init("@Url.Action("GetListSysRole")", "@AdminHelper.GetPageSize()");

    CommonHelper.SetHeadingTitle("Role Management");

    /* =========================== user role =========================== */

    DynaTreeHelper.TreeData = @Html.Raw(Json.Encode(ViewBag.FeatureDynaTree));
    DynaTreeHelper.LoadManageUrl = "@Url.Action("GetRole_FeatureList")";
    DynaTreeHelper.ManageUrl = "@Url.Action("ManageRole_Feature")";

    DynaTreeHelper.GetArrId = function (result) {
        var arrId = new Array();
        
        for (var i = 0; i < result.length; i++)
        {
            var id = result[i];
            arrId.push(id);
        }

        return arrId;
    }

    DynaTreeHelper.Init();
</script>